package com.qian.apiserver.controller;

import com.qian.apiserver.model.Order;
import com.qian.apiserver.model.OrderModel;
import com.qian.apiserver.model.User;
import com.qian.apiserver.model.Withdrawal;
import com.qian.apiserver.service.IOrderService;
import com.qian.apiserver.service.IUserService;
import com.sun.xml.internal.rngom.parse.host.Base;
import com.yaowk.api.common.BaseException;
import com.yaowk.api.common.PageReq;
import com.yaowk.api.common.PageResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/game/order")
@Api(tags = "充值记录")
public class OrderController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IUserService userService;

    @PostMapping("/save")
    @ApiOperation("修改状态")
    public void save(Byte status){
        Order order = new Order();
        order.setStatus(status);
        orderService.save(order);
    }
    @GetMapping("/all")
    @ApiOperation("充值订单(不填所有订单 时间查询 用户编号查询userCode 支付方式查询 状态查询)")
    public Map find(PageReq pageReq, OrderModel model){

        if(!StringUtils.isEmpty(model.getUserName())){
            Pattern pattern = Pattern.compile("[0-9]*");
            if(pattern.matcher(model.getUserName()).matches()){
                User user = new User();
                user.setUserCode(Long.parseLong(model.getUserName()));
                user = userService.findOne(user);
                model.setUserId(user.getId().longValue());
                return orderService.find(pageReq,model);
            }else{
                User user = new User();
                List list = new ArrayList();
                user.setUsername(model.getUserName());
                PageResp<User> pageResp = userService.find(pageReq,user);
                for(User user1:pageResp.getList()){
                    model.setUserId(user1.getId().longValue());
                    list.addAll(orderService.findName(pageReq,model));
                }
                Map map = new LinkedHashMap();
                map.put("totalPage",pageResp.getTotalPage());
                map.put("totalRow",pageResp.getTotalRow());
                map.put("pageSize",pageResp.getPageSize());
                map.put("pageNumber",pageResp.getPageNumber());
                map.put("list",list);
                return map;
            }
        }
        return orderService.find(pageReq,model);
    }

    @GetMapping("/orderAmount")
    @ApiOperation("充值总金额")
    private BigDecimal orderAmount(){
        List<Order> order = orderService.findAll();
        Double money = 0.0;
        for(Order order1:order){
            money += order1.getMoney().doubleValue();
        }
        return new BigDecimal(money);
    }
}
